import 'package:flutter/cupertino.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyDialog.dart';
import 'package:imember/Widget/MyImage.dart';
import 'package:imember/Widget/MyInput.dart';

import '../CameraScan.dart';
import 'ScanReceiveConfirm.dart';

class ScanReceive extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ScanReceive();
  }
}

class _ScanReceive extends State {
  List<String> orderList = [""];

  addInput() {
    setState(() {
      orderList.add("");
    });
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> orderInputList = [];
    List<TextEditingController> orderInputControllerList = [];
    for (var i = 0; i < orderList.length; i++) {
      orderInputControllerList.add(TextEditingController());
      orderInputList.add(OrderNoInput(orderInputControllerList[i],onDelClick: () {
        setState(() {
          orderList.removeAt(i);
        });
      }, onCodeClick: () {
        showWidgetDialog(context, CameraScan((value){
          orderInputControllerList[i].text = value;
        }));
      }));
    }
    return Container(
        color: MyColor.White,
        width: 600,
        child: SingleChildScrollView(
            child: Column(mainAxisSize: MainAxisSize.min, children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            LocalImage("ic_back_g.png", width: 48, onTap: () {
              PageUtil().back(context);
            }),
            Text("掃描領貨", style: TextStyle(fontSize: 20, color: MyColor.C51)),
            Container(width: 48),
          ]),
          Container(height: 1, color: MyColor.C4, margin: EdgeInsets.only(bottom: 36)),
          Column(mainAxisSize: MainAxisSize.min, children: orderInputList),
          LocalImage("ic_add_input.png", width: 24, padding: EdgeInsets.all(10), onTap: addInput),
          OrangeBtn("確認", width: 170, height: 37, margin: EdgeInsets.fromLTRB(0, 38, 0, 26),onTap: (){
            PageUtil().back(context);
            showWidgetDialog(context, ScanReceiveConfirm());
          })
        ])));
  }
}

class OrderNoInput extends StatelessWidget {
  TextEditingController controller;
  Function() onDelClick;
  Function() onCodeClick;

  OrderNoInput(this.controller,{required this.onDelClick, required this.onCodeClick});

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.only(top: 12),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              width: 430,
              height: 37,
              margin: EdgeInsets.only(right: 8),
              padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
              decoration: BoxDecoration(
                  color: MyColor.F5,
                  borderRadius: BorderRadius.all(Radius.circular(24)),
                  border: Border.all(width: 1.2, color: MyColor.C70)),
              child: NormalInput(hintText: "請輸入訂單編號或掃碼",controller: controller),
            ),
            LocalImage("ic_code.png", width: 20, padding: EdgeInsets.all(4), onTap: onCodeClick),
            LocalImage("ic_x_g.png", width: 30, padding: EdgeInsets.all(4), onTap: onDelClick),
          ],
        ));
  }
}
